package com.minshenglife.zanzan.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.zanzan.dao.EMOUserGroupDao;
import com.minshenglife.zanzan.dao.UserDao;
import com.minshenglife.zanzan.entity.EMOUserGroup;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.entity.eoa.LA_AGENT;
import com.minshenglife.zanzan.entity.eoa.V_EMP;
import com.minshenglife.zanzan.pub.Keys;
import com.minshenglife.zanzan.service.AccessTokenService;
import com.minshenglife.zanzan.service.EMOUserGroupService;

/**
 * emo用户分组绑定
 * @author jianghaibo
 *
 */
@Component
@Transactional
public class EMOUserGroupServiceImpl implements EMOUserGroupService{

	Logger log = LoggerFactory.getLogger(EMOUserGroupServiceImpl.class);
	@Resource
	EMOUserGroupDao emoUserGroupDao;
	@Resource
	AccessTokenService accessTokenService;
	@Resource
	UserDao userDao;
	@Override
	public EMOUserGroup getUserByOpenid(String userOpenId) {
		EMOUserGroup emoUserGroup = new EMOUserGroup();
		emoUserGroup.setOpenid(userOpenId);
		List<EMOUserGroup> findByExample = emoUserGroupDao.findByExample(emoUserGroup);
		if(findByExample != null && findByExample.size() > 0){
			return findByExample.get(0);
		}
		return null;
	}

	@Override
	public boolean isBind(String empNo, String openId) {
		EMOUserGroup emoUserGroup = new EMOUserGroup();
		emoUserGroup.setEmpId(empNo);
		emoUserGroup.setOpenid(openId);
		List<EMOUserGroup> list = emoUserGroupDao.findByExample(emoUserGroup);
		if(list != null && list.size() > 0){
			return true;
		}
		return false;
	}
	//{"group":{"id":103,"name":"内勤"}}
	//{"group":{"id":104,"name":"外勤"}}
	@SuppressWarnings("rawtypes")
	@Override
	@Transactional
	public void bind(String openid, String appid, Object data) {
		EMOUserGroup emoUserGroup = new EMOUserGroup();
		emoUserGroup.setAppid(appid);
		emoUserGroup.setOpenid(openid);
		Map map = (Map) data;
		String empType = (String) map.get("type");
		emoUserGroup.setEmpType(empType);
		V_EMP ve = null;
		LA_AGENT agent = null;
		User user = new User();
		if(empType.equals("1")){	//内勤
			ve = (V_EMP) map.get("user");
			emoUserGroup.setWxGroupId("103");
			emoUserGroup.setEmpId(ve.getEmpId());
			
			user.setEmpId(ve.getEmpId());
			List<User> userList = userDao.findByExample(user);
			boolean isExist = userList != null && userList.size() > 0 ? true : false;
			if(!isExist){
				//同步内勤员工数据
				user.setEmpId(ve.getEmpId());
				user.setEmp_name(ve.getEmpName());
				user.setEmpType(ve.getEmployeeType());
				user.setEmp_status(ve.getEmp_status());
				user.setDept_id(ve.getDeptId());
				user.setSex(ve.getGender());
				user.setIdNo(ve.getIdNo());
				user.setBirthday(ve.getBirthday());
				user.setStature(ve.getStature());
				user.setWeight(ve.getWeight());
				user.setBlood_type(ve.getBlood_type());
				user.setHire_date(ve.getHire_date());
				user.setTelephone(ve.getTelephone());
				user.setMobilePhone(ve.getMobilePhone());
				user.setWork_email(ve.getWorkMail());
				user.setUser_state(Keys.USER_STATE_ZERO);
				user.setUser_type(Keys.USER_TYPE_ONE);
				user.setIntegralNum(new Long(0));
				user.setRedFlowerNum(new Double(0));
				user.setFdFlowerNum(new Double(0));
				user.setSendFlowerNum(new Long(0));
				user.setCanSendNum(new Long(0));
				user.setCanTuCaoNum(new Long(0));
				userDao.add(user);
			}
		}else if(empType.equals("2")){	//外勤
			agent = (LA_AGENT) map.get("user");
			emoUserGroup.setWxGroupId("104");
			emoUserGroup.setEmpId(agent.getAgentCode());
			
			user.setEmpId(agent.getAgentCode());
			List<User> userList = userDao.findByExample(user);
			boolean isExist = userList != null && userList.size() > 0 ? true : false;
			if(isExist){
				log.debug("员工数据已经同步");
				System.out.println("员工数据已经同步");
			}else{
				log.debug("员工数据未同步，开始同步------");
				System.out.println("员工数据未同步，开始同步------");
			}
			if(!isExist){
				//同步外勤员工数据
				user.setEmp_name(agent.getName());
				user.setEmpType(Keys.USER_EMP_TYPE_AGENT);
				user.setSex(agent.getSex());
				user.setIdNo(agent.getIdNo());
				user.setBirthday(agent.getBirthday());
				user.setBlood_type(agent.getBloodType());
				user.setHire_date(agent.getEmployDate());
				user.setTelephone(agent.getPhone());
				user.setMobilePhone(agent.getMobile());
				user.setWork_email(agent.getEmail());
				user.setUser_state(Keys.USER_STATE_ZERO);
				user.setUser_type(Keys.USER_TYPE_ZERO);
				user.setIntegralNum(new Long(0));
				user.setRedFlowerNum(new Double(0));
				user.setFdFlowerNum(new Double(0));
				user.setSendFlowerNum(new Long(0));
				user.setCanSendNum(new Long(0));
				user.setCanTuCaoNum(new Long(0));
				
				userDao.add(user);
			}
		}
		Long id = emoUserGroupDao.findMaxId();
		if(id != null){
			id += 1l;
		}else{
			id = 1l;
		}
		emoUserGroup.setId(id);
		emoUserGroupDao.add(emoUserGroup);
	}

	@Override
	public void EmpUnbound(String empId, String openid) {
		EMOUserGroup emoUserGroup = new EMOUserGroup();
		emoUserGroup.setEmpId(empId);
		emoUserGroup.setOpenid(openid);
		emoUserGroupDao.deleteByExample(emoUserGroup);
		
	}


}
